var paymentGroup = 0;
var paymentS = 0;
var paymentCharge = 0;
var paymentTax = 0;
var paymentChargeGroup = 0;
var paymentTaxGroup = 0;
var userPayment = 0;
var sumAllPayment = 0;
var ChargesGroupOwner_checkbox ="";
//for payment button in group page
function ShowPaymentPage()
{
	//Newss Responsibility
	var Tabs = getCurrentTabData();
	if(Tabs!=undefined){
		Tabs.page = "Payment";
		waitingDialog({});
		if(Tabs.Payment == null)
		{
			loadGroupPaymentService();
		}
	}else{
		loadGroupPaymentService();
	}
}
//Prepare Service
function loadGroupPaymentService(){
	var services = [];
	var Tabs = getCurrentTabData();
	if(Tabs!=undefined){
		if(SharedData.PayModeAndPayType==null) 
			services.push(["/paytypeService",{func:"GetAllPayModeAndPayType"},function(data,args) { SharedData.PayModeAndPayType = JSON.parse(data); },null]);
		services.push(["/folioService?",{func:"loadFolioList",id:Tabs.resId},load_Group_folio_success,Tabs]);
		arrayservice(services,DisplayGroupPaymentPage,null);
	}
}

function load_Group_folio_success(data,Tabs)
{
	Tabs.FolioList = JSON.parse(data);
}
//Display
function DisplayGroupPaymentPage(){
	//Newss Responsibility
	CheckResBeforePayment();
	closeWaitingDialog();
	DisplayDetail();
}
function DisplayGroupPaymentDetail(){
	$("#payment-dialog").css("display","");
	$("#payment-dialog").css("z-index","100000");
	fillDataInGroupPayment();
}
function fillDataInGroupPayment(){
	fillPaymentMode();
	fillGroupPaymentTitle();
	fillGroupPaymentOwner();
	fillRoomDetailInGroupAccStateMent();
	fillPaymentInGroupAccStateMent();
	fillChargeInGroupAccountStatement(1,null);
	fillAllcost();
	ShowFolio();
	var Tabs = getCurrentTabData();
	if(Tabs.folioPaymentType!=null)
	{
		var res =Tabs.ResIdpaymentIngroup;
		LoadSinglePaymentInGroupPage(Tabs.folioPaymentType,res,Tabs);
	}
	if(Tabs.folioInPaymentPage != null)
	{
		var folioId= Tabs.folioInPaymentPage;
		var type= Tabs.folioPaymentType;
		var resCheck = Tabs.resCheck;
		LoadFolioDataInPayment(folioId,type,Tabs,resCheck);
	}
	showPaymentcommandbutton();
	closeWaitingDialog();
}
function fillGroupPaymentTitle(){
	var Tabs = getCurrentTabData();
	$('[displaytype=group]').css("display","");
	$('[displaytype=single]').css("display","none");
	var name = (Tabs.Owner != null && Tabs.Owner.firstName != null)? Tabs.Owner.firstName : "-";
	name += (Tabs.Owner != null && Tabs.Owner.lastName!=null)? Tabs.Owner.lastName : "";
	$("[name=edit_headform]:visible [name=guestname]").text(name);
	$("[name=edit_headform]:visible [name=agent]").text((Tabs.Agency.agency == '')? "-" : Tabs.Agency.agency);
	$("[name=edit_headform]:visible [name=groupcode]").text(Tabs.resId);
	$("[name=edit_headform]:visible [name=resstatus]").text(getLocalizeResvStatus(Tabs.ResData.reservationStatusName));
}

function fillPaymentMode(){
		var selected = $("select[name=paymode]:visible");
		selected.find("option").remove();
		for(var i=0;i<SharedData.PayModeAndPayType.paymode.length;i++)
		{
			var paymode = SharedData.PayModeAndPayType.paymode[i];
			selected.append('<option value="'+paymode.id+'">'+paymode.title+'</option>');
		}
		fillPayPaymentType();
	}

function fillPayPaymentType(){
	$("select[name=paytype]:visible").find("option").remove();
	var paymodeId = $("select[name=paymode]").find("option:selected").val();
	var list = getListFromDataIndex(SharedData.PayModeAndPayType.paytype,"modeString",paymodeId)
	for(var i=0;i<list.length;i++){
		$("select[name=paytype]:visible").append(optionString.format(list[i].id,list[i].description));
	}
	changetopayment();
}

function fillGroupPaymentOwner(){
	var Tabs = getCurrentTabData();
	if(Tabs.type == "Group"){
		var name = (Tabs.Owner != null && Tabs.Owner.firstName != null)? Tabs.Owner.firstName : "-";
		name += (Tabs.Owner != null && Tabs.Owner.lastName!=null)? " "+Tabs.Owner.lastName : "";
		$('table[name=bookingDetail] tr[displaytype=group]').css("display","none");
		$('table[name=bookingDetail] tr[displaytype=single]').css("display","");
		$('table[name=bookingDetail] span[name=bookingId]').text(booking_detail_label);
		$('#Payment_bookingdetails_groupowner').text(name);
		$('#Payment_bookingdetails_guestId').text((Tabs.Owner!= null)?Tabs.Owner.id:"-");
		$('#Payment_bookingdetails_Identificationnumber').text((Tabs.Owner!= null)?Tabs.Owner.idNumber:"-");
		$("[id*=Payment_bookingdetails_company]").text((Tabs.Owner!= null)?Tabs.Owner.company:"-");
		$('#Payment_bookingdetails_address').text((Tabs.Owner!= null)?Tabs.Owner.addressLine1:"-");
		$('#Payment_bookingdetails_tel').text((Tabs.Owner!= null)?Tabs.Owner.phone:"");

		var rooms = Tabs.GroupResvList;
		var roomString="";
		var adult=0;
		var child=0;
		roomString += "(";
		for(var j=0;j<rooms.length;j++){
			adult+=rooms[j].ResData.adult;
			child+=rooms[j].ResData.child;
		roomString += rooms[j].ResData.roomName + ",";
		}
		roomString = roomString.substring(0,roomString.length-1)+")";
		$('#Payment_bookingdetails_checkIndetail_roomtype').text(getRoomTypeId(Tabs.ResData.roomtypeString).shortName+roomString);
		$('#Payment_bookingdetails_checkIndetail_date').text(
			new Date(Tabs.ResData.date).getFormatedString() + " - " + new Date(Tabs.ResData.date).addDate(Tabs.ResData.span).getFormatedString() +
			"("+ Tabs.ResData.span +" "+ simpleLabel_Night +")");
		$('#Payment_bookingdetails_built').text(new Date(Tabs.ResData.date).getFormatedString());
		$('#Payment_bookingdetails_checkIndetail_rate').text(getObjectFromSharedData(SharedData.Package,Tabs.ResData.priceRateString).Name);
		$('#Payment_bookingdetails_amountroomandguest_roomamount').text(rooms.length);
		$('#Payment_bookingdetails_amountroomandguest_adult').text(adult);
		$('#Payment_bookingdetails_amountroomandguest_children').text(child);
	}
}

function fillRoomDetailInGroupAccStateMent(roomsResAry)
{	
	$("[id*=route-new-folio-item]").find($("option[name]")).remove();
	var Tabs = getCurrentTabData();
 	$("#route-new-folio-item").append( $("<option name=\"groupId\">").val(Tabs.resId).html(Tabs.Owner.firstName+" "+Tabs.Owner.lastName+"("+ownerGroupLag+")"));
	var rooms = Tabs.GroupResvList;
	if(roomsResAry!=null){rooms=roomsResAry}
	var arrayDetailResList="";
	for(var i=0;i<rooms.length;i++)
	{
		$("#route-new-folio-item").append( 
			$("<option name=\"resId\">").val(rooms[i].resId).html(
			(rooms[i].guest!=null)?rooms[i].guest.Name:""
			+"("+rooms[i].ResData.roomTypeName
			+rooms[i].ResData.roomName+")")
		);
		var DetailResStateMent = rooms[i].ResData.roomTypeName+" "+rooms[i].ResData.roomName
		+"/"+new Date(rooms[i].ResData.date).getFormatedDateAndMonthString() + " - " 
		+ new Date(rooms[i].ResData.date).addDate(rooms[i].ResData.span).getFormatedDateAndMonthString()+"/"
		+rooms[i].resId;
		var datelill = rooms[i].ResData.date;
		if(datelill == null)
			datelill = rooms[i].ResData.date;
		$("#foliogroup_singlepayment_footer_btn").show();
		$("#back_to_folio_btn").hide();
		$("#innerfoliogroup_singlepayment_footer_btn").hide();
		$('[name*=StateMentListSingle]').remove();
		$('[name*=chargeandpayment]').remove();
		arrayDetailResList +='<tr name="StateMentListSingle" class="payment-hover-roompayment" id ="Single'+rooms[i].resId+'" onclick="LoadSinglepaymentinGroupPage(this)">'+
			'<td  reservationId ="'+rooms[i].resId+'"  name="resrooms">'+
			'<span>'+new Date(datelill).getFormatedString()
			+"/"+new Date(datelill).getHours()+":"
			+new Date(datelill).getMinutes()+'</span>'+
			'</td>'+
			'<td><span>'+DetailResStateMent+'</span></td>'+
			'<td>&nbsp;</td>'+
			'<td>&nbsp;</td>'+
			'<td>&nbsp;</td>'+
			'<td>&nbsp;</td>'+
			'<td>&nbsp;</td>'+
			'<td>&nbsp;</td>'+
			'</tr>'+
			'<tr name="chargeandpayment'+rooms[i].resId+'"></tr>'+
			fillChargeInGroupAccountStatement(null,rooms[i]);
		if($("[id*=Single"+rooms[i].resId+"]").size() ==0)
			$('#paymentResInGroup').after(arrayDetailResList);
	}
	$('[name=payment_footer_btn').not('[id]').show();
	$("[id=payment-folio-list]").show();
}

function fillPaymentInGroupAccStateMent(groupPayment,resPayment)
{
	var Tabs = getCurrentTabData();
	var paymentList = Tabs.PaymentList;
	if(resPayment!=null){paymentList=resPayment}
	var groupPaymentList = Tabs.GroupPaymentList;
	if(groupPayment!=null){groupPaymentList=groupPayment}
	if(groupPaymentList != null)
	{
		var aryGroupPaymentList ="";
		for(var i =0;i<groupPaymentList.length;i++)
		{
			 aryGroupPaymentList ='<tr name="StateMentListSingle" class="SinglepaymentList'+Tabs.resId+'" style="background-color:#F7F9A2">'+
					'<td>'+
					'<span><input resId ="Single'+Tabs.resId+'"  value="'+groupPaymentList[i].id+'" type="checkbox" name="checkbox_payment_group"></span><span>'+new Date(groupPaymentList[i].payDate).getFormatedString()+'</span>'+
					'</td>'+
					'<td><span>'+groupPaymentList[i].description+'</span></td>'+
					'<td>&nbsp;</td>'+
					'<td>&nbsp;</td>'+
					'<td>&nbsp;</td>'+
					'<td><span style="float:right;">'+toCurrencyString(0,false)+'</span></td>'+
					'<td><span>&nbsp;</span></td>'+
					'<td><span style="float:right;">'+toCurrencyString(groupPaymentList[i].amount,false)+'</span></td>'+
					'</tr>';
			 if($("[value*="+groupPaymentList[i].id+"]").parents("tr").size()==0){$('#paymentListGroup').before(aryGroupPaymentList);}
		}
		
	}
	if(paymentList != null)
	{
		var arrayPaymentList ="";
		for(var i =0;i<paymentList.length;i++)
		{
			arrayPaymentList = ('<tr name="StateMentListSingle" class="SinglepaymentList'+Tabs.resId+'" style="background-color:#F7F9A2">'+
					'<td>'+
					'<span><input resId ="Single'+Tabs.resId+'"  value="'+paymentList[i].id+'" type="checkbox" name="checkbox_payment_res"></span><span>'+new Date(paymentList[i].payDate).getFormatedString()+'</span>'+
					'</td>'+
					'<td><span>'+paymentList[i].description+'</span></td>'+
					'<td><span style="color:#3b69b3;">&nbsp;</span></td>'+
					'<td><span>&nbsp;</span></td>'+
					'<td><span style="float:right;">&nbsp;</span></td>'+
					'<td><span style="float:right;">0.00</span><span style="float:right;">&nbsp;B&nbsp;</span></td>'+
					'<td><span>&nbsp;</span></td>'+
					'<td><span style="float:right;">'+parseFloat(paymentList[i].amount).toFixed(currencyDecimalPlaces)+'</span><span style="float:right;">&nbsp;B&nbsp;</span></td>'+
					'</tr>');
			 if($("[value*="+paymentList[i].id+"]").parents("tr").size()==0){$('#paymentResInGroup').after(arrayPaymentList);}
		}
	}
}

function fillChargeInGroupAccountStatement(groupCharge,resCharge)
{
	var Tabs = getCurrentTabData();
	var chargeList = Tabs.ChargeList;
	var arrayChargeList ="";
	var arrayText="";
	var chargeName="Singlecheckbox_charge_group";
	var resIdElm = Tabs.resId;
	if(resCharge!=null)
	{
		chargeList=resCharge.ChargeList;
		resIdElm = resCharge.resId;	
		chargeName="Singlecheckbox_charge_resing";
	}else
	{
		$('[name*=Singlecheckbox_charge_group]').parents("tr").remove();
	}
	if(chargeList == null)
		return;
	
	for(var i =0;i<chargeList.length;i++)
	{
		var disPlayFolioStatus="";
		if(!chargeList[i].isTemp){
		var folioNumber="";
		var q ="";
		var rc="";
		if(chargeList[i].Qty != parseInt(0))
		{
			q=Qty+" "+chargeList[i].Qty+" ";
			rc=roomCharge+"/";
		}
		if(chargeList[i].FolioId !=0)
		{
			disPlayFolioStatus="PI";
			folioNumber = chargeList[i].FolioId;
		} 
		arrayChargeList = '<tr name="StateMentListSingle" class=SinglechargeList'+Tabs.resId+' >'+
		'<td>'+
		'<span><input resId = "'+resIdElm+'" value="'+chargeList[i].id+'" folioId='+chargeList[i].FolioId+' cost="'+parseFloat(+chargeList[i].chargeAmount).toFixed(currencyDecimalPlaces)+'" type="checkbox" name="'+chargeName+'" ></span><span>'
		+new Date(chargeList[i].chargeDate).getFormatedString()+'</span>'+
		'</td>'+
		'<td><span>'+q+'</span><span name="roomcharge_refund">'+rc+'</span><span id=description'+i+'>'+chargeList[i].description+'</span></td>'+
		'<td><span style="color:#3b69b3;" name="folio_number_onshow">'+disPlayFolioStatus+" "+folioNumber+'</span></td>'+
		'<td><span>&nbsp;</span></td>'+
		'<td><span id=charge'+i+' charge="'+chargeList[i].chargeAmount+'">'+toCurrencyString(chargeList[i].chargeAmount,false)+'</span></td>'+
		'<td><span id=tax'+i+' tax="'+chargeList[i].tax+'" >'+toCurrencyString(chargeList[i].tax,false)+'</span></td>'+
		'<td><span>&nbsp;</span></td>'+
		'<td><span>&nbsp;</span></td>'+
		'</tr>';
			if($("[value*="+chargeList[i].id+"]").parents("tr").size()==0){
				if(chargeList[i].isRefund)
				{
	 				var chargeText = $(arrayChargeList);
					var checkboxItem= chargeText.children(":first").find("span:first input");
					checkboxItem.attr("disabled", true);
					var refundText= chargeText.clone();
					refundText.find("[name=roomcharge_refund]").text(refundCharge+"/");
					refundText.children(":first").find("span:first input").removeAttr("name");
					$('#chargeListGroupSingle').after('<tr name="StateMentListSingle" class=SinglechargeList'+Tabs.resId+' >'
					+chargeText.html()
					+'</tr>'
					+'<tr name="StateMentListSingle" class="isrefund" >'+refundText.html()+'</tr>');
				}
				else
				if(groupCharge != null)
				{
					$('[id*=paymentListGroup]').after(arrayChargeList);
				}
				else
				{
					 arrayText+=arrayChargeList
					if(i==chargeList.length-1)
					{
						return arrayText;
					}
				
				}
				
			}
		}
	}
}

function expireDisplayGroupPaymentDetail(data)
{	
	$($("[id*=accList]")[0]).show();
	$("[id*=payment_groupId]")[0].innerHTML="&nbsp;";
	$("#accList").find($("[id*=chargeListGroup]")).remove();	
	$("#accList").find($('[name=StateMentListSingle]')).remove();
	$("#accList").find($('[class=payment-hover-roompayment]')).remove();
	$("#accList").find($('[class*=paymentListGroup]')).after('</tr>'+
			'<tr id="chargeListGroup">'+
			'</tr>'+
			'<tr class="DeteailchargeList" style="background-color:#CCC">'+
			'</tr>');
//	ShowAccountStateMentListGroup();
//	ShowAccountStateMentList();
//	clearPayment();
//	ShowFolio();
}
function ClosePaymentPage() {
	CloseTapPage(getCurrentTab().attr('title'));
}

function addPayment(id) {
	if($('#paymode'+id.split("").pop()).val() == "Chose"){eeeeeee
		simpleDialog("please chose pay mode ",100,null);
		return false;
	}
	if($('#paymode'+id.split("").pop()).val() == "Cash" || $('#paymode'+id.split("").pop()).val() == "CreditCard"){
		if($('#paytype'+id.split("").pop()).val() == "0"){
			simpleDialog("please chose paytpye",100,null);
			return false;
		}
	}
	var payMode = $("[name=paymode]:visible").val();
	var payType = $("[name=paytype]:visible").val();
	var payamount = $('#payamount'+id.split("").pop()).val();
	var creditId=$('select#payCcNo'+id.split("").pop()).val();
		if(creditId == null){
			creditId="0";
		}
	var payCcNo = $('#payCcNo'+id.split("").pop()).val();
	var payCcNo2 = $('#payCcNo2'+id.split("").pop()).val();
	var payDes = $('#payDes'+id.split("").pop()).val();
	var link = "/paymentService";
	var typePage = getCurrentTabData().type;
	var resInGroup = getCurrentTabData().ResIdpaymentIngroup;
	if(typePage=="Group"){
		if(resInGroup!=null){
			var res = resInGroup;
			var groupId = "0";
		}else{
			var res = "0";
			var groupId = getCurrentTabData().resId;
		}
	}else{
		var res = getCurrentTabData().resId;
		var groupId = "0";
	}
	
	var data = { func : "addPayment",
			paymode: payMode,
			paytype: payType,
			amount: payamount,
			ccNo: payCcNo,
			ccNo2: payCcNo2,
			creditCardId:creditId,
			description: payDes,
			groupId: groupId,
			resId: res
		};
	simpleservice(link,data,addPaymentSuccess);
}

function addPaymentSuccess(data){
	var Tabs = getCurrentTabData();
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = CheckSession(objArray);
	if(checkLogOut)
		return;
	if(data == null){
		simpleDialog(saveFail_label,100,null);
	}else{
		if(Tabs.type!="Single"){
			Tabs.GroupPaymentList.push(objArray);
			fillPaymentInGroupAccStateMent();
		}else{
			if(Tabs.PaymentList == null){Tabs.PaymentList=[];}
			Tabs.PaymentList.push(objArray);
			fillPaymentInAccountStateMent();
		}
	}
}

function clearPayment(){
	paymentGroup = 0;
	paymentS=0;
	paymentTax = 0;
	userPayment = 0;
	paymentCharge = 0;
	$('#payment-charges-group_owner')[0].options.length = 1;
}

function checkAll(){
	if($('#accList tr [type=checkbox]')[0].checked == true){
		for(var i = 1;i<$('#accList tr [type=checkbox]').length;i++){
			$('#accList tr [type=checkbox]')[i].checked = true;
		}
	}else{
		for(var i = 1;i<$('#accList tr [type=checkbox]').length;i++){
			$('#accList tr [type=checkbox]')[i].checked = false;
		}
	}
}

function setPOSSelection(){
	var url = "/POSCategoriesService";
	var data = { func : "getPOS"}
	simpleserviceWithArgs(url,data,setPOSSelectionSuccess);
}
function setPOSSelectionSuccess(data){
	var objArray = JSON.parse(data); 
	$("[name=payment-charges-pos] option").remove();
	$("[name=payment-charges-pos]").append('<option>' + selectPOS + '</option>');
	for (key in objArray[0]) {
		$("[name=payment-charges-pos]").append('<option value="' + objArray[0][key].id + '">' + objArray[0][key].Title + '</option>');
	}
}

function setItemPos(){
	var posId = $("[name=payment-charges-pos]:visible").val();
	var url = "/POSItemService";
	var data = { func : "loadItem",posId:posId}
	simpleserviceWithArgs(url,data,setItemPosSuccess);
}
function setItemPosSuccess(data){
	var objArray = JSON.parse(data);
	$("[name=payment-chrages-charges_listchb] option").remove();
	$("[name=payment-chrages-charges_listchb]").append('<option>' + selectItem + '</option>');
	for (key in objArray) {
		$("[name=payment-chrages-charges_listchb]").append('<option price="'+objArray[key].price+'" value="' + objArray[key].id + '">' + objArray[key].title + '</option>');
	}
}
function clearPOSItem(){
	$("[name=payment-chrages-charges_listchb] option").remove();
	$("[name=payment-chrages-charges_listchb]").append('<option>' + selectItem + '</option>');
	$("[name=payment-charges-price]:visible").html(toCurrencyString(0));
	$("[name=payment-charges-price]:visible").attr("price",0);
}

function setPriceItem(){
	var itemId = $("[name=payment-chrages-charges_listchb]:visible").val();
	if(itemId!=null){
		var url = "/POSItemService";
		var data = { func : "getPOSItemById",itemId:itemId}
		simpleserviceWithArgs(url,data,setPriceItemSuccess);
	}else{
		$("[name=payment-charges-price]:visible").html(toCurrencyString(0));
		$("[name=payment-charges-price]:visible").attr("price",0);
	}
	
}
function setPriceItemSuccess(data){
	var objArray = JSON.parse(data);
	$("[name=payment-charges-price]:visible").html(toCurrencyString(objArray.price));
	$("[name=payment-charges-price]:visible").attr("price",objArray.price);
}

function CreateChargesdiaLog(name)
{
	var Tabs = getCurrentTabData();
	var headName ="";
	if(name == "btn_create_charges")
	{
		headName =othercharges;
		$("#payment-charges-list").val('');
		$("[name=selectPOS]").show();
		clearPOSItem();
		setPOSSelection();
		$("#payment-other_charges").show();
		$("#payment-custom_charges").hide();
		$("[name=payment-chrages-charges_listchb]").show();
		$("#payment-charges-list").hide();
		$("[name=payment-charges-price]").show();
		$("#payment-charges-inputprice").hide();

		
	}
	else if(name == "btn_create_custom_charges")
	{
		headName=customcharges;
		$("#payment-charges-list").val('');
		$("[name=selectPOS]").show();
		clearPOSItem();
		setPOSSelection();
		$("#payment-other_charges").hide();
		$("#payment-custom_charges").show();
		$("[name=payment-chrages-charges_listchb]").hide();
		$("#payment-charges-list").show();
		$("[name=payment-charges-price]").hide();
		$("#payment-charges-inputprice").show();

		
	}
	else if(name == "btn_create_custom_charges_earyCheckin")
	{
		$("#payment-other_charges").hide();
		$("#payment-custom_charges").show();
		$("[name=payment-chrages-charges_listchb]").hide();
		$("#payment-charges-list").show();
		$("[name=payment-charges-price]").hide();
		$("#payment-charges-inputprice").show();
	}
		var buttons = [
			{ title:btnSave , callback: function(thisdialog,args){
				EditCharge();
				thisdialog.dialog("close");
				//callCancelReaservation();
			}},
			{ title:btnCancel , callback: function(thisdialog,args){
				thisdialog.dialog("close");
			}}
		];
		
		customDialog(headName,$("[id=payment_charges_popup]").html(),{ width: 750},buttons,null);
		$("[id*=payment-charges-group_owner]").find($("option")).remove();
		
		if(Tabs.type == "Single")
		{
			$("[id*=payment-charges-group_owner]").append( $("<option>").val(value=Tabs.ResData.roomString).html(value=Tabs.ResData.roomName));
		}if(Tabs.type == "Group")
		{
			$("[id*=payment-charges-group_owner]").append( $("<option restype=\""+Tabs.type+"\">").val(value=Tabs.ResData.id).html(value=ownerGroupLag));
			for(var i=0;i<Tabs.GroupResvList.length;i++){
				$("[id*=payment-charges-group_owner]").append( $("<option restype=\"Single\">").val(value=Tabs.GroupResvList[i].ResData.id).html(value=Tabs.GroupResvList[i].ResData.roomName));
				
			}
		}
}

function CreateRefundDialog()
{
	var z = new Array();
	for(var i= 0;i<$("input[name*=checkbox_charge_res]:visible:checked").size();i++){
		z[i] = $($("input[name*=checkbox_charge_res]:visible:checked")[i]).attr("name").split("").pop();
	}
	if(z.length == 0){
		alert("checked box to refund");
		return false;
	}
	$("[name*=refunditem]:visible").replaceWith("<tr style=\"background-color:#B5B7F9; display:none;\" id=\"refundlist\" ><td>&nbsp;</td></tr>");
	$("#payment-refund-item-totle").html("0.00");
	LoadRefundItem();
	 
	 var buttons = [
					{ title:btnSave , callback: function(thisdialog,args){
						AddRefund();
				thisdialog.dialog("close");
						//callCancelReaservation();
					}},
					{ title:btnCancel , callback: function(thisdialog,args){
						thisdialog.dialog("close");
					}}
				];
				customDialog(refund,$("[id=payment-refund]").html(),{ width: 389},buttons,null);	 
}

function BacktoGroupPaymentPage()
{
	$("#accList").find($("[id*=chargeListGroup]")).remove();
	$("[name=edit_groups]").css("display","none");
	$("[name=edit_none_groups]").css("display","none");
	$('[id*=tappage_roommanage_menu_editreserved].select').attr("paymentpage","group_payment");
	$('[id*=tappage_roommanage_menu_editreserved].select').removeAttr("folioId");
	
	$("#single-payment-dialog").css("display","none");
	$("#payment-dialog").show();
	$("#single-payment-info").css("display","none");
	$("#single-payment-ingroup-info").css("display","none");
	$("#accList").find($('[name=StateMentListSingle]')).remove();
	$("#accList").find($('[class=payment-hover-roompayment]')).remove();
	$("#accList").find($('[class*=paymentListGroup]')).replaceWith('</tr>'+
			'<tr id="paymentListGroup">'+
			'</tr>'+
			'<tr id="chargeListGroup">'+
			'</tr>'+
			'<tr class="DeteailchargeList" style="background-color:#CCC">'+
			'</tr>');
	ShowAccountStateMentList();
	ShowFolio();
}

function BacktoDetail()
{
	if($("div[forload=editreserved] [name=editReserved_group]").size() == 0){
		waitingDialog({});
		var div = $("<div>");
		$("div[forload=editreserved]").append(div);
		div.load("components/EditReserved/EditReserved.jsp",function(){
			callBacktoDetail()
		});
	}
	else{
		callBacktoDetail()
	}
	
	function callBacktoDetail(){
		var Tabs = getCurrentTabData();
		Tabs.page ="Detail";
		for(var i= 0;i<Tabs.ChargeList.length;i++)
		{
			if(!Tabs.ChargeList[i].isRoomCharge)	
				//delete Tabs.ChargeList[i];
			Tabs.ChargeList.splice(i,1);
		}
		DisplayDetail();
	}
}

function LoadSinglepaymentinGroupPage(e)
{	
	var Tabs = getCurrentTabData();
	Tabs.folioPaymentType="PaymentSingleinGroupPage";
	var res =$(e).children('td:first').attr('reservationid');
	Tabs.ResIdpaymentIngroup=res;
	LoadSinglePaymentInGroupPage(Tabs.folioPaymentType,res,Tabs);
}

function LoadSinglePaymentInGroupPage(folioPaymentType,res,Tabs)
{
	
	var folioAryList=null;
	var chargeAryList=null;
	var paymentAryList=null;
	for(var i=0;i<Tabs.GroupResvList.length;i++)
	{
		if(Tabs.GroupResvList[i].resId == res){
			var roomsResAry=[Tabs.GroupResvList[i]];
			 folioAryList=Tabs.GroupResvList[i].FolioList;
			 chargeAryList=Tabs.GroupResvList[i].ChargeList;
			 paymentAryList=Tabs.GroupResvList[i].paymentList;
		}
	}
	if(folioPaymentType=="PaymentSingleinGroupPage"){
		ShowFolio(folioAryList);
		fillRoomDetailInGroupAccStateMent(roomsResAry);
		fillChargeInGroupAccountStatement(chargeAryList);
		fillPaymentInGroupAccStateMent(null,paymentAryList);
	}
	$('[name*=Singlecheckbox_charge_group]').parents('tr').remove();
	$('[name*=checkbox_payment_group]').parents('tr').remove();
}

function Verifycheckout()
{
	var Tabs = getCurrentTabData();
	if(Tabs.FolioList==null){simpleDialog("long the folio!!",100,null); return;}
	for(var i=0;i<Tabs.ChargeList.length;i++)
	{
			if(getCurrentTabData().ChargeList[i].FolioId==0)
				simpleDialog("Create the folio!!",100,null); 
				return;
	}
	for(var i=0;i<Tabs.FolioList.length;i++)
	{
		if(!getCurrentTabData().FolioList[i][0][0].isLock)
			simpleDialog("long the folio!!",100,null); 
			return;
	}
	if(Tabs.calBalance - Tabs.calBalance == 0){
		if(Tabs.type == "Single")
		{
			btnCheckOut();
		}else
		{
			btnCheckOutAllGroup();
		}
	}else
	{
			simpleDialog("long the folio!!",100,null);
	}
}

function VerifyExpresscheckout()
{
	var Tabs = getCurrentTabData();
	for(var i=0;i<Tabs.ChargeList.length;i++)
	{
			if(getCurrentTabData().ChargeList[i].FolioId==0)
				simpleDialog("Create the folio!!",100,null); 
				return;
	}
	if(Tabs.type == "Single")
	{
		btnCheckOut();
	}else
	{
		btnCheckOutAllGroup();
	}
}

function printPayment(type){
	$.getScript("/js/print/printPayment.js")
	  .done(function( script, textStatus ) {
		  printDataPayment(type);
	  })
}
function printPaymentGroup(type){
	$.getScript("/js/print/printPaymentGroup.js")
	  .done(function( script, textStatus ) {
		  printDataPaymentGroup(type);
	  })
}

function showPaymentcommandbutton(){
	var Tabs = getCurrentTabData();
	var canPolicyTime =sortDateCancelPolicy()[sortDateCancelPolicy().length-1];
	if(Tabs.resId == 0)
	{
		if(Tabs.page=="Payment")
		{
			$(".command_button_payment[name=btn_backtodetail]").show();
			$(".command_button_payment[name=btn_close_payment]").show();
			(Tabs.type == "Single")?$(".command_button_payment[name=btn_print_payment_single]").show()
					:$(".command_button_payment[name=btn_print_payment]").show();	
		}
	}else if(Tabs.resId != 0)
	{
		var status = Tabs.ResData.reservationStatusName;
		if(Tabs.page=="Payment" && status == "Temp Rooms"){
			if(Tabs.fromGroupId != null){
				$(".command_button_payment[name=btn_Group_payment]").show();
			}
			$(".command_button_payment[name=btn_backtodetail]").show();
			$(".command_button_payment[name=btn_close_payment]").show();
			(Tabs.type == "Single")?$(".command_button_payment[name=btn_print_payment_single]").show()
					:$(".command_button_payment[name=btn_print_payment]").show();
		}else if(status == "Reserved Rooms"||status == "Checked-in Rooms"){
			if(Tabs.fromGroupId != null){
				$(".command_button_payment[name=btn_Group_payment]").show();
			}else{
				$(".command_button_payment[name=btn_settle_folio]").show();
			}
			$(".command_button_payment[name=btn_backtodetail]").show();
			$(".command_button_payment[name=btn_close_payment]").show();
			(Tabs.type == "Single")?$(".command_button_payment[name=btn_print_payment_single]").show()
					:$(".command_button_payment[name=btn_print_payment]").show();
			if(Tabs.ResData.lateCheckout&&getServerDateTime().getTime()> getEarlyCheckOut().getTime()
					||Tabs.ResData.EarlyChecKout&&getServerDateTime().getTime()< getEarlyCheckOut().getTime()){
				$(".command_button_payment[name=btn_expresscheckout_payment]").show();
				if(Tabs.type != "Group"){
					$(".command_button_payment[name=btn_checkout_payment]").show();
				}else{
					$(".command_button_payment[name=btn_payment_checkOutG]").show();
				}
			}else if(getServerDateTime().getTime()< getEarlyCheckOut().getTime()){
				$(".command_button_payment[name=btn_payment_latecheckOut]").show();
				
			}
		}else if(status =="Checked-out Rooms"){
			if(Tabs.fromGroupId != null){
				$(".command_button_payment[name=btn_Group_payment]").show();
			}else{
				if((totalAmount+(Tabs.calBalance.otherTax+Tabs.calBalance.roomsTax))-Tabs.calBalance.payprice ==0)
				$(".command_button_payment[name=btn_settle_folio]").show();
			}
			$(".command_button_payment[name=btn_backtodetail]").show();
			$(".command_button_payment[name=btn_close_payment]").show();
			(Tabs.type == "Single")?$(".command_button_payment[name=btn_print_payment_single]").show()
					:$(".command_button_payment[name=btn_print_payment]").show();
		}
	}
}

function fillCreditCardnoPayment(valSlt){
	var Tabs = getCurrentTabData();
	if(Tabs.type !="Group"){
		var selected = $('select[id=payCcNoS]:visible');
	}else{
		var selected = $("select[id=payCcNoG]:visible");
	}
	selected.find("option").remove();
	selected.append("<option value=''>"+simpleLabel_Select+"</option>");
	if(Tabs.CreditCard!=null){
		for(var i=0;i<Tabs.CreditCard.length;i++)
			selected.append("<option value='"+Tabs.CreditCard[i].id+"'>"+Tabs.CreditCard[i].NameOnCard+"</option>");
		(valSlt!=null)?selected.val(valSlt):selected.val(0);
	}
	selected.append('<option value="AddCreditCard">'+addcreditCard+'</option>');
}

function checkToAddCreditCard(){
	var Tabs = getCurrentTabData();
	var check="";
	(Tabs.type != "Single")?check = $('select[id=payCcNoG]:visible').val():check = $('select[id=payCcNoS]:visible').val();
	if(check == "AddCreditCard"){
		AddcreditCard();
	}
}

function changetopayment(){
	var Tabs = getCurrentTabData();
	var check = $('select[name=paymode]:visible').val();
	var payMode =getObjectFromSharedData(SharedData.PayModeAndPayType.paymode,check).title;
	if(payMode == "Credit Card"){
		if(Tabs.type != "Group"){
			$('select[id=payCcNoS]').css("display","");
			$('input[id=payCcNoS]').css("display","none");
		}else{
			$('select[id=payCcNoG]').css("display","");
			$('input[id=payCcNoG]').css("display","none");
		}
		fillCreditCardnoPayment();
	}else{
		if(Tabs.type != "Group"){
			$('select[id=payCcNoS]').css("display","none");
			$('input[id=payCcNoS]').css("display","");
		}else{
			$('select[id=payCcNoG]').css("display","none");
			$('input[id=payCcNoG]').css("display","");
		}
		
	}
}